<?php
session_start(); 

//Stops error messages being disp;layed
//Used as deletes all images and if the image doesnt exist it errors
error_reporting(E_ERROR);

include "webmaster_connect.php";

$product_row_id			= $_POST['product_row_id'];
$product_id			= $_POST['product_id'];
$product_type 			= $_POST['product_type'];
$product_catagory 		= $_POST['product_catagory'];
$product_sub_catagory	= $_POST['product_sub_catagory'];
$product_name 			= $_POST['product_name'];
$product_status 		= $_POST['product_status'];
$product_cost 			= $_POST['product_cost'];
$product_description	= $_POST['product_description'];
$product_make 			= $_POST['product_make'];
$product_colour 		= $_POST['product_colour'];
$product_size 			= $_POST['product_size'];
$product_offer 			= $_POST['product_offer'];

$query = "SELECT * FROM saddles WHERE id = $product_row_id";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
	$previuos_status = $row['product_status'];
	$link = $row['link'];
	$saddle_name = $row['product_name'];
	$saddle_row_id = $row['id'];
}


mysql_query("UPDATE saddles SET 


product_name = '$product_name',
product_status = '$product_status',
product_cost = '$product_cost', 
product_description = '$product_description',
product_make = '$product_make', 
product_colour = '$product_colour', 	
product_size = '$product_size',
product_offer = '$product_offer'

WHERE id = $product_row_id");
	
	//////////////////////////////
	//Send alert email to customer
	//////////////////////////////
	
	//	1. Previous status for product was "On-Trial"
	//	2. New status for product is "Available"
	//	3. Find all clients that have an alert set up for this product
	//	4. Send the alert email to all those clients
	
	// 1 & 2 //
	//Checks if the saddle was changed from SOLD or ON TRIAL to AVAILABLE
	if($_POST['product_status'] == "Available" && $previuos_status == "On-trial" ) 

	{
		
		/////////////////////////////////////
		//Get customer details and send email
		/////////////////////////////////////
		
		// 3 //
		$query = "SELECT * FROM customer_alerts WHERE product_id = '$product_id'";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result))
		{
			
		$guest_email = $row['customer_email'];
		
		$to      = $guest_email;
		$subject = 'Saddle alert created';
			
		$message = file_get_contents('admin_files/emails/customer_alert.html');
		$message = $change = str_replace("{SADDLE_NAME}", $saddle_name, $message);
		$message = $change = str_replace("{SADDLE_ROW_ID}", $saddle_row_id, $message);
		$message = $change = str_replace("{PRODUCT_LINK}", $link, $message);
						
		$headers = "From: 24-7Saddles.com <Sales@24-7saddles.com>\r\n";
		$headers .= "MIME-Version: 1.0\r\n";
		$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
		
		// 4 //
		mail($to, $subject, $message, $headers);			
		
		}
				
		/////
		//End
		//Get customer details and send email
		/////
				
	;}
	
	/////
	//End
	//Send alert email to customer
	/////
	
	/////////////////////////////////
	//Remove alerts if saddle is sold
	/////////////////////////////////
	
	// 1. If the new product status = "Sold"
	// 1. Delete all customer alerts for that product
	// 2. Deletes the product sale images
	// 3. Deletes the product sale page
	
	if($_POST['product_status'] == "Sold") 

	{
			
		mysql_query("DELETE FROM customer_alerts WHERE product_id = '$product_id'");
	
		////////////////////////////
		//Deletes the product images
		////////////////////////////
	
		//Delete the product images
		$ftp_server="ftp.24-7saddles.com";
		$ftp_user_name="z247s504";
		$ftp_user_pass="PxLpCd07";
	
		// set up basic connection
		$conn_id = ftp_connect($ftp_server);
	
		// login with username and password
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
	
		// change directory to public_html
		ftp_chdir($conn_id, 'public_html/images/saddles');

		// try to delete $file
		ftp_delete($conn_id, $product_row_id."_1.jpg");
		ftp_delete($conn_id, $product_row_id."_2.jpg");
		ftp_delete($conn_id, $product_row_id."_3.jpg");
		ftp_delete($conn_id, $product_row_id."_4.jpg");
	
		//////////////////////////
		//Deletes the product page
		//////////////////////////
	
		// set up basic connection
 		$conn_id = ftp_connect($ftp_server);

	 	// login with username and password
	 	$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

    	ftp_chdir($conn_id, 'public_html/saddles');
	
		//Gets the product name
		$page_name = $product_name;
		//Gets the page name
		$page_name = preg_replace('/\s+/', '_', $page_name);
	
		//Deletes the page
		ftp_delete($conn_id, $page_name.".php");
		
		}
	
		/////
		//End
		/////

//	1. Set current product id as session active product id
$_SESSION['active_product_id'] = $product_row_id;

//	2. Set the cookie message
setcookie("save_message", "Product details updated", time()+15);

//	3. Take user back to amend products page
header("Location: webmaster_amend_products.php");

//////
//Test
//////

echo $product_type."<br />";		
echo $product_catagory."<br />"; 		
echo $product_sub_catagory."<br />";	
echo $product_name."<br />"; 			
echo $product_status."<br />"; 		
echo $product_cost."<br />"; 			
echo $product_description."<br />";	
echo $product_make."<br />"; 			
echo $product_colour."<br />"; 		
echo $product_size."<br />"; 			
echo $product_offer."<br />";

echo $new_product_id."<br />";


echo $acronym."<br />";

echo "Previous Status: ".$previuos_status."</br>";
echo "Product Status: ".$_POST['product_status']."</br>";
echo "Link: ".$link.'</br>';
echo "To: ".$to;


?>